import { service, crudParamsSerializer } from '@/utils/request'
import { CreateQueryParams } from '@nestjsx/crud-request'
import { Song } from './song.dto'
import { CrudPaginatedRes } from '..'

const defaultSongData: Partial<Song> = {
  title: '',
  duration: 0,
  favCount: 0,
  playedCount: 0,
  visible: true
}

function getSongs (params?: CreateQueryParams) {
  return service.get<Song[] | CrudPaginatedRes<Song>>(
    '/resources/songs',
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

function getSongById (id: number | string, params?: CreateQueryParams) {
  return service.get<Song>(
     `/resources/songs/${id}`,
     {
       params,
       paramsSerializer: crudParamsSerializer
     }
  )
}

function checkFavSong (id: number | string) {
  return service.request({
    url: `/resources/songs/${id}/fav`,
    method: 'get'
  })
}

function setFavSong (id: number | string) {
  return service.request({
    url: `/resources/songs/${id}/fav`,
    method: 'post'
  })
}

function removeFavSong (id: number | string) {
  return service.request({
    url: `/resources/songs/${id}/fav`,
    method: 'delete'
  })
}

export { defaultSongData, getSongs, getSongById, checkFavSong, setFavSong, removeFavSong }
